package com.sale.plugins;

import com.apache.api.manager.ProxyManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.database.constant.SpringContextLoader;
import com.apache.exception.BusinessException;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.UtilPlugin;
import com.apache.tools.DataMap;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 判断申购唯一性：一个交易商同一个发售活动只能申购一次
 *
 * @author admin
 */
public class UniqueBuyForSalePluginImpl implements CustomMethodPlugin {
    private Logger log = Logger.getLogger(getClass());

    @SuppressWarnings("rawtypes")
    @Override
    public Object beforeInvoke(Map<String, Object> param) {
        //封装参数
        Map<String,Object> whereMap = new HashMap<String,Object>();
        whereMap.put("trsBuy.w_saleId", param.get("trsBuy.saleId"));
        whereMap.put("trsBuy.w_userId", param.get("trsBuy.userId"));
        List<DataMap> list = UtilPlugin.getInstance().getListByParams(String.valueOf(param.get("datasource")), "s_trsBuy", whereMap);
        if (list != null && list.size() > 0) {
            throw new BusinessException(
                    "[" + param.get("trsBuy.proName") + "-" + param.get("trsBuy.proNo")
                            + "]您已经申购成功，不能重复申购！");
        }
        return null;
    }

    @Override
    public Object doInvoke(Map<String, Object> param) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> param) {

        // TODO Auto-generated method stub
        return null;
    }

    /**
     * 规则校验入口（专门给系统公式规则提供的）
     *
     * @param paramsVo
     */
    public void run(ParamsVo paramsVo) {
        beforeInvoke(paramsVo.getParams());
    }
}
